import { useEffect } from 'react'

import { createCyCategoryApi, updatecyCategoryApi } from '@/api/pcs/cyCategory'
import AekForm from '@/components/AekForm'
import AekModal, { PromiseThen } from '@/components/AekModal'
import { requestWithTry } from '@/utils/request'

const EditModal = ({ row = {} }) => {
  const [form] = AekForm.useForm()
  const { id } = row

  useEffect(() => {
    if (!row.id) return
    form.setFieldsValue({
      ...row,
    })
  }, [row.id])

  /** @type {import("@/components/AekForm").FormDataProps[]} */
  const formData = [
    {
      field: 'name',
      label: '分类名称',
      formItemProps: {
        rules: [{ required: true }],
      },
      component: { name: 'Input' },
    },
  ]

  const onOk = async () => {
    const values = await form.validateFields()
    const params = {
      id,
      ...values,
    }
    await requestWithTry(id ? updatecyCategoryApi : createCyCategoryApi, params)
  }
  return (
    <AekModal title={id ? '编辑' : '新增'} width={500} onOk={onOk}>
      <AekForm form={form} formData={formData} />
    </AekModal>
  )
}

EditModal.PromiseThen = PromiseThen

export default EditModal
